module repository.RedEnvelopeRecordRepository;

import bootstrap.DatabaseBootstrap;
import hunt.entity;
import hunt.logging;
import std.conv : to;
import model;
import message.MsgDefine;
import core.stdc.time;

import hunt.framework;

class RedEnvelopeRecordRepository : EntityRepository!( RedEnvelopeRecord , int){

  this()
  {
    super( defaultEntityManager());
  }


  int insertRecord(RedEnvelope envObj,RedEnvelopeSplit splitObj,ulong userId,EntityManager em)
  {
    auto insert = em.createQuery!(RedEnvelopeRecord)("INSERT INTO RedEnvelopeRecord u (u.envelopeId,u.dynamic,u.created,u.chatId,u.userId,u.fromId) VALUES (:envelopeId,:dynamic,:created,:chatId,:userId,:fromId);");
    insert.setParameter("envelopeId",envObj.id);
    insert.setParameter("dynamic",splitObj.amount);
    insert.setParameter("created",core.stdc.time.time(null));
    insert.setParameter("chatId",envObj.chatId);
    insert.setParameter("userId",userId);
    insert.setParameter("fromId",envObj.fromId);
    int id = insert.exec();

    return id;
  }

  RedEnvelopeRecord[] getAllById(int envelopeId)
  {
     return findAll(new Condition("%s = %s" , Field.envelopeId , envelopeId));
  }
}
